/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ly.count.ohos.demo;

import ly.count.ohos.sdk.Countly;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.global.configuration.Configuration;

import java.util.HashMap;

/**
 * 首页
 *
 * @since 2021-04-13
 */
public class MainAbility extends Ability {
    private String demoTag = "CountlyDemo";

    private ScrollView sv;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        sv = (ScrollView)findComponentById(ResourceTable.Id_sv);
        sv.enableScrollBar(ScrollView.AXIS_Y,true);

        findComponentById(ResourceTable.Id_button8).setClickedListener(this::onClickButtonCrashReporting);
        findComponentById(ResourceTable.Id_button7).setClickedListener(this::onClickButtonUserDetails);
        findComponentById(ResourceTable.Id_button6).setClickedListener(this::onClickButtonApm);
        findComponentById(ResourceTable.Id_button5).setClickedListener(this::onClickButtonViewTracking);
        findComponentById(ResourceTable.Id_button3).setClickedListener(this::onClickButtonMultiThreading);
        findComponentById(ResourceTable.Id_button52).setClickedListener(this::onClickButtonRemoteConfig);
        findComponentById(ResourceTable.Id_button59).setClickedListener(this::onClickButtonDeviceId);
        findComponentById(ResourceTable.Id_button70).setClickedListener(this::onClickButtonRatings);
        findComponentById(ResourceTable.Id_button2).setClickedListener(this::onClickButtonOthers);
        findComponentById(ResourceTable.Id_button9).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onClickButtonCustomEvents(component);
            }
        });
    }

    private void onClickButtonCustomEvents(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleCustomEvents.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonCrashReporting(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleCrashReporting.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonUserDetails(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleUserDetails.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonApm(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleApm.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonComponentTracking(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleViewTracking.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonViewTracking(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleViewTracking.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonMultiThreading(Component v) {
    }

    private void onClickButtonOthers(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleOthers.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonRemoteConfig(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleRemoteConfig.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonDeviceId(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleDeviceId.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void onClickButtonRatings(Component v) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(AbilityExampleFeedback.class)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void enableCrashTracking() {
        // add some custom segments, like dependency library versions
        HashMap<String, String> data = new HashMap<>();
        data.put("Facebook", "3.5");
        data.put("Admob", "6.5");
        Countly.sharedInstance().setCustomCrashSegments(data);
        Countly.sharedInstance().enableCrashReporting();
    }

    @Override
    public void onStop() {
        Countly.sharedInstance().onStop();
        super.onStop();
    }

    @Override
    public void onConfigurationUpdated(Configuration configuration) {
        super.onConfigurationUpdated(configuration);
        Countly.sharedInstance().onConfigurationChanged(configuration);
    }
}
